package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    /*
     * 添加购物车
     * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1.查询当前添加的商品在当前用户的购物车列表中是否存在
        //select *from shopping_cart  where user_id？ and dish_id？ and dish_flavor=？
        //select *from shopping_cart  where user_id？ and setmeal_id=?

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 目前查询最多查询到一个商品信息。方法的复用性，设计成返回一个集合：查询用户的购物车列表可以复用此方法
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByCondition(shoppingCart);

        //2.如果存在，更新当前商品的数量：数量+1
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //update shopping_cart set number = number +1 where user_id=?...
            //2.1构建更新的购物车实体对象
            ShoppingCart updateShoppingCart = ShoppingCart.builder()
                    .number(shoppingCartList.get(0).getNumber() + 1)
                    .id(shoppingCartList.get(0).getId())
                    .build();
            //2.2更新购物车
            shoppingCartMapper.update(updateShoppingCart);//number / id
            return;
        }

        //添加的是菜品信息
        //select * from dish where id= ？
        ShoppingCart addShoppingCart = new ShoppingCart();
        addShoppingCart.setNumber(1);
        addShoppingCart.setCreateTime(LocalDateTime.now());
        addShoppingCart.setUserId(BaseContext.getCurrentId());

        //3.1 判断当前添加的是菜品：
        if (shoppingCartDTO.getDishId() != null) {
            //添加的是菜品信息
            //select * from dish where id= ？
            Dish dish = dishMapper.getById(shoppingCart.getDishId());

            addShoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            addShoppingCart.setDishId(dish.getId());
            addShoppingCart.setImage(dish.getImage());
            addShoppingCart.setName(dish.getName());
            addShoppingCart.setAmount(dish.getPrice());
        } else {
            //3.2 判断当前添加的是套餐：setmeal_id
            //select * from setmeal where id= ？
            Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());

            addShoppingCart.setSetmealId(setmeal.getId());
            addShoppingCart.setImage(setmeal.getImage());
            addShoppingCart.setName(setmeal.getName());
            addShoppingCart.setAmount(setmeal.getPrice());
        }

        //3.3封装购物车实体对象，添加购物车信息
        //insert inio shopping_cart values()
        shoppingCartMapper.insert(addShoppingCart);

    }

    /*
     * 查询购物车
     *
     * */
    @Override
    public List<ShoppingCart> shoppingCart() {

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByCondition(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
        return shoppingCartList;
    }

    /*
     * 清空购物车
     * */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /*
     * 删除购物车中一个商品
     * */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //非空判断
        if (shoppingCartDTO.getDishId() == null && shoppingCartDTO.getSetmealId() == null) {
            throw new DeletionNotAllowedException(MessageConstant.UNKNOWN_ERROR);
        }
        //获取此商品在表中的详细信息
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //当前用户id,保证有且只有一条符合的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.selectByCondition(shoppingCart);
        //非空判断
        if (list.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.UNKNOWN_ERROR);
        }
        //根据条件查询有且只有一条数据
        ShoppingCart modify = list.get(0);

        //判断此时数量是否大于1
        if (modify.getNumber() > 1) {
            //大于1 则减去1
            modify.setNumber(modify.getNumber() - 1);
            //更新数据
            shoppingCartMapper.update(modify);
        } else {
            //小于1 则移除
            shoppingCartMapper.deleteById(modify);
        }
    }
}

